package com.luo.simple.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.luo.simple.entity.User;
import com.luo.simple.entity.UserExample;
import com.luo.simple.entity.result.Result;
import com.luo.simple.entity.result.ResultBuilder;
import com.luo.simple.mapper.UserMapper;
import com.luo.simple.parms.webParms.UserLoginParm;
import com.luo.simple.parms.webParms.UserParm;
import com.luo.simple.service.UserService;
import com.luo.simple.util.IdUtil;
import com.luo.simple.util.PhoneUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private PhoneUtil phoneUtil;

    /**
     * 注册用户
     *
     * @param userParm 用户前端接收参数
     * @return Result 返回值封装
     */
    @Override
    public Result registerUser(UserParm userParm) {
        String message = checked(userParm);
        if (!StringUtils.isEmpty(message)) {
            return ResultBuilder.genFailResult(message);
        }
        User user = userParm.generateUser();
        user.setUserId(IdUtil.generateId());
        user.setUserPass(SecureUtil.md5(SecureUtil.md5(userParm.getPassword())));
        userMapper.insert(user);
        return ResultBuilder.genSuccessResult("用户注册成功");

    }

    @Override
    public User selectByPrimaryKey(String userId) {
        return userMapper.selectByPrimaryKey(userId);

    }

    /**
     * 用户登录
     *
     * @param userLoginParm 用户前端接收参数
     * @return Result 返回值封装
     */
    @Override
    public Result loginUser(UserLoginParm userLoginParm) {
        UserExample example = new UserExample();
        UserExample.Criteria cc = example.createCriteria();
        cc.andUserPhoneEqualTo(userLoginParm.getUsername());
        cc.andUserPassEqualTo(SecureUtil.md5(SecureUtil.md5(userLoginParm.getPassword())));
        List<User> users = queryAllUser(example);
        if (users != null && !users.isEmpty()) {
            return ResultBuilder.genSuccessResult(users.get(0));
        }
        return ResultBuilder.genFailResult("用户登录失败");
    }

    /**
     * 根据手机号查询当前用户信息是否存在
     *
     * @param phone 用户手机号
     * @return Result 返回值封装
     */
    @Override
    public List<User> queryUserByPhone(String phone) {
        UserExample example = new UserExample();
        UserExample.Criteria cc = example.createCriteria();
        cc.andUserPhoneEqualTo(phone);
        return queryAllUser(example);
    }

    /**
     * 根据手机号查询当前用户信息是否存在
     *
     * @param example 查询参数模型
     * @return List<User>  用户集合
     */
    @Override
    public List<User> queryAllUser(UserExample example) {
        return userMapper.selectByExample(example);
    }

    /**
     * 参数验证信息
     *
     * @param userParm 用户前端参数
     * @return String  返回错误信息
     */
    private String checked(UserParm userParm) {
        if (StringUtils.isEmpty(userParm.getTelephone())) {
            return "手机号不能为空";
        }
        if (StringUtils.isEmpty(userParm.getPassword())) {
            return "密码不能为空";
        }
        if (StringUtils.isEmpty(userParm.getUserAderss())) {
            return "用户钱包地址不能为空";
        }
        if (userParm.getPassword().length() < 30) {
            return "用户密码不能超过30位";
        }
//        if (StringUtils.isEmpty(userParm.getUserSpreadId())) {
//            return "推广人id不能为空";
//        }
        if (!phoneUtil.compilePhoneRegex(userParm.getTelephone())) {
            return "您的手机号" + userParm.getTelephone() + "不为有效手机号";
        }
        List<User> userList = queryUserByPhone(userParm.getTelephone());
        if (userList != null && !userList.isEmpty()) {
            return "该手机号" + userParm.getTelephone() + "已经注册";
        }
        //如果输入了推广人id--则验证推广人id是否存在
        if (!StringUtils.isEmpty(userParm.getUserSpreadId())) {
            User user = selectByPrimaryKey(userParm.getUserSpreadId());
            if (user == null) {
                return "推广人id" + userParm.getUserSpreadId() + "不存在";
            }
        }
        return null;
    }
}
